{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b467f9d7-725f-4621-aa71-9b18f0f75da7",
   "metadata": {},
   "source": [
    "# 列表\n",
    "### 列表的应用场景\n",
    "生活中的：学生信息，企业人事信息，学习档案信息等\n",
    "### 列表的格式\n",
    "<code>[数据1，数据2，数据3，数据4，......]<./br>\n",
    "### 列表可以一次性地存储多个数据，而且可以为不同数据放置不同的内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "605571a0-a05e-48ab-9491-45b8cb940402",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n",
      "李四\n",
      "王五\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "name_list = ['张三', '李四', '王五']   # 这个列表放置的是同一种数据类型\n",
    "name_list_new = [11155, '张三', 9985, '李四']   # 这个列表放置的是不同的数据类型\n",
    "# 列表可以无限长度\n",
    "# 打印列表当中的元素\n",
    "print(name_list[0])    # 打印第一个元素\n",
    "print(name_list[1])\n",
    "print(name_list[2])\n",
    "#print(name_list[4])    # list index out of range 因为没有第四个元素，所以就会出现列表下表越界异常"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43c9bfb1-b68e-438b-8cdd-d74b69c27183",
   "metadata": {},
   "source": [
    "以上的输出打印当时非常不便，后面会用循环遍历来解决这个问题，此处只是为了演示列表是有下标的\n",
    "\n",
    "index()内置函数，返回指定数据所在位置的下表\n",
    "列表序列.index(数据， 开始位置下表， 结束位置下标)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c6259e3-2a50-439c-b326-1da182c8cd31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四']\n",
    "print(name_list.index('李四', 0, 4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49913b9a-e5f9-4663-ac30-1600e25186a9",
   "metadata": {},
   "source": [
    "- count() 内置函数，表示统计\n",
    "- len(列表对象名) 内置函数，表示长度\n",
    "- 这两个函数都可以在列表当中使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2dc3da83-e25d-4035-9ea5-8135f4bda6ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [111555, '张三', 9988, '李四', '李四', '李四']\n",
    "print(name_list.count('李四'))    #统计当前列表中“李四”出现的次数\n",
    "print(len(name_list))    #打印当前列表的元素的长度，从1开始统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bef5d76-b48d-4396-b945-9e9ca51ca2aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "in() 内置函数，可以判断某某是否存在在列表中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6c3cc741-74e6-4a58-a1f0-615dc11cea85",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四']\n",
    "print('李四' in name_list)     # True\n",
    "print('老许' in name_list)     # False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "673ed4c5-6539-4852-bcc5-86cea84a278a",
   "metadata": {},
   "source": [
    "not in() 可以判断某某某在列表中不存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ad888c02-9ee7-4438-b08b-e2731bdb7752",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "print('李四' not in name_list)     # False\n",
    "print('老许' not in name_list)     # True"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5b6db2f-6033-405c-b06b-0994a058a852",
   "metadata": {},
   "source": [
    "查找用户输入的名字是否已经存在于列表当中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2571eec5-b99e-41f8-be95-e40fe2d49473",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入您的名字： 李四\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您输入的名字是李四, 名字已经存在，已被学校录取\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name = input('请输入您的名字：')\n",
    "# while True:    # 无限循环， 表示程序可以无限输出\n",
    "if name in name_list:\n",
    "    print(f'您输入的名字是{name}, 名字已经存在，已被学校录取')\n",
    "else:\n",
    "    print(f'您输入的名字是{name}, 名字不存在，未被学校录取')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b261aea2-518e-44ca-89f3-8ff8da38c9ed",
   "metadata": {},
   "source": [
    "### 增加操作\n",
    "- append(): 指定内容追加到列表的结尾\n",
    "- 列表序列.append(数据)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "db512650-43c4-4833-907d-83ba0d189a80",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11155, '张三', 9988, '李四', '李四', '李四', '王五', '小明']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list.append('小明')\n",
    "\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11716097-fad7-4df7-9e78-31515e8b5ed2",
   "metadata": {},
   "source": [
    "输了能够追加单个数据外，还可以一次性地追加多个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "65fe2624-249d-4e02-9f75-0b77f877d6b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11155, '张三', 9988, '李四', '李四', '李四', '王五', ['小明', '赵六', '钱七']]\n",
      "王五\n",
      "['小明', '赵六', '钱七']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list.append(['小明', '赵六', '钱七'])\n",
    "\n",
    "print(name_list)\n",
    "\n",
    "print(name_list[6])\n",
    "print(name_list[7])   #append()将多个元素放置到列表当中后，将其当成一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8b2c965-88d7-4ff0-9748-4b22f2ce0de9",
   "metadata": {},
   "outputs": [],
   "source": [
    "extend() 表示列表结尾追加数据，如果数据是一个序列，则将这个序列的数据逐一添加到列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "96eeb426-56b7-4a89-a8d8-87f20bb8e410",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11155, '张三', 9988, '李四', '李四', '李四', '王五', '小明', '赵六', '钱七']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list.extend(['小明', '赵六', '钱七'])\n",
    "\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64376bd9-e23e-422d-a181-3a103747ab82",
   "metadata": {},
   "source": [
    "> append() 和extend()函数的 区别：\n",
    "> - append()和extend()都是将数据追加到列表当中，但是append()追加的时候，会将多个元素当成一个元素追加到列表中，会形成列表嵌套\n",
    "> - extend()它就是将多个元素逐一的放置到列表当中"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27f802cc-f4fe-4022-b10b-90b35a8cb37b",
   "metadata": {},
   "source": [
    "- <code>insert()</code> 内置函数指定位置新增数据\n",
    "- 列表序列.insert(设置下标， 数据)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bd533e85-4cfc-46f1-ac8c-29c05be2beb6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11155, '老许', '张三', 9988, '李四', '李四', '李四', '王五']\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list.insert(1, '老许')\n",
    "\n",
    "print(name_list)\n",
    "print('老许' not in name_list)\n",
    "print('老许' in name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fafb0c3-db85-46bc-9153-6b706980b7ae",
   "metadata": {},
   "source": [
    "### 删除\n",
    "- del 内置函数就是删除列表的存在\n",
    "- del 目标<.code>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a40512c1-ab47-4a9b-9e19-ff67e02a8062",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'name_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[13], line 5\u001b[0m\n\u001b[0;32m      2\u001b[0m name_list \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m11155\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m张三\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;241m9988\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m李四\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m李四\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m李四\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m王五\u001b[39m\u001b[38;5;124m'\u001b[39m]\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m name_list\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(name_list)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'name_list' is not defined"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "del name_list\n",
    "\n",
    "print(name_list)   # name "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35a4cdb4-ac21-4a4e-89d8-244d935e1ceb",
   "metadata": {},
   "source": [
    "删除指定数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a3753721-d1f3-40fd-b635-8257ad238a1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三', 9988, '李四', '李四', '李四', '王五']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "del name_list[0]\n",
    "\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2f45b87-2fe4-434c-8e15-7de607ab5048",
   "metadata": {},
   "source": [
    "- <code>pop()</code>内置函数，表示删除指定下标的数据（默认为最后一个，并返回该数据）\n",
    "- 列表序列.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "dea8f58b-5ae2-461f-ad40-f1243ea033c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "王五\n",
      "张三\n",
      "[11155, 9988, '李四', '李四', '李四']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "\n",
    "del_name = name_list.pop()   # 如果不设置，就默认弹出结尾\n",
    "print(del_name)\n",
    "\n",
    "del_name = name_list.pop(1)   \n",
    "print(del_name)\n",
    "\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c371d408-6765-4e7f-b2c6-641c7919f895",
   "metadata": {},
   "source": [
    "- remove() 函数 移除列表中某一个数据的第一个匹配项\n",
    "- 列表序列.remove(数据)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9648db07-7b7a-45ef-9336-cbbc1d0d9c6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11155, '张三', 9988, '李四', '李四', '王五']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "\n",
    "name_list.remove('李四')   # 有相同元素的情况下，默认删除的是第一个元素\n",
    "\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "411698dc-97f3-417d-8bfd-ae50099f77e2",
   "metadata": {},
   "source": [
    "- clear()函数 清空列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6fa0c7ed-b1b2-4f86-959a-1056beb2f544",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "\n",
    "name_list.clear()\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8873da8-c4f3-472a-b065-46e512881d94",
   "metadata": {},
   "source": [
    "### 修改\n",
    "- 修改执行下标数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "76426e80-56ef-4587-a050-6b3abe9d3534",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['aaa', '张三', 9988, '李四', '李四', '李四', '王五']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list[0] = 'aaa'\n",
    "\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "551337a8-c2b9-48c5-8c0a-8d26c7626243",
   "metadata": {},
   "source": [
    "- reverse() 函数 逆置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e2a6abf7-cb52-4a2c-b8c5-f93c86528e4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['王五', '李四', '李四', '李四', 9988, '张三', 11155]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list.reverse()\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f01c9101-060c-4f53-908d-51ee12c7e898",
   "metadata": {},
   "source": [
    "- sort()函数 排序\n",
    "- 列表序列.sort( key = None, reverse = false)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "347b1757-1ce1-4603-8eb0-576e6c24a686",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 44, 67, 69, 77, 99, 999]\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "# name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list = [1, 99, 67, 44, 69, 77, 999]\n",
    "\n",
    "name_list.sort()   # 排序操作（从小~大进行排序）\n",
    "print(name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e450c9c-41e0-4630-9d84-902586cf2b9b",
   "metadata": {},
   "source": [
    "- copy() 复制操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "0c6ac282-7378-4786-94cd-5c11559b21e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11155, '张三', 9988, '李四', '李四', '李四', '王五']\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "name_list_2 = name_list.copy()\n",
    "\n",
    "print(name_list_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da776840-0ec8-4e6b-87e7-f2624726d9d3",
   "metadata": {},
   "source": [
    "# 列表的循环遍历\n",
    "既然是列表，那么肯定要通过训练遍历操作的（一个一个的数数字/点名）\n",
    "所谓遍历实际上就是点名操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0e8aa1ac-9759-4de8-a7dd-91f3e49df0ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11155\n",
      "张三\n",
      "9988\n",
      "李四\n",
      "李四\n",
      "李四\n",
      "王五\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = [11155, '张三', 9988, '李四','李四','李四', '王五']\n",
    "\n",
    "i = 0\n",
    "while i < len(name_list):\n",
    "    print(name_list[i])\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95dc7e3d-f46b-4db5-9fa4-f52d292c7f9a",
   "metadata": {},
   "source": [
    "# 列表嵌套"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "628cee58-d123-47fe-965e-94532b2c9212",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张三', '李四', '王五']\n",
      "李四\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "name_list = list = [['小明','小红','小蓝'],['Tom', 'Lily', 'Rose'],['张三', '李四', '王五']]\n",
    "\n",
    "# 如果要找到 李四的位置 首先要找到李四对应的列表的位置\n",
    "print(name_list[2])     # ['张三', '李四', '王五']]\n",
    "\n",
    "# 第二步 从李四所在的列表里面，再按下标找到数据李四\n",
    "print(name_list[2][1])      # 李四"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fa8f662b-773b-462f-8b8c-84ccfba042cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "办公室1的人数是0, 老师分别是：\n",
      "办公室2的人数是6, 老师分别是：张三1...张三2...张三3...张三5...张三7...张三8...\n",
      "办公室3的人数是2, 老师分别是：张三4...张三6...\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "import random   # 导入随机数库\n",
    "\n",
    "teacher = ['张三1', '张三2', '张三3', '张三4', '张三5', '张三6', '张三7', '张三8']\n",
    "offices = [[], [], []]\n",
    "# for循环中变量vartest随意定义a、b、c、d都行，但是teacher必须跟上面的列表名称容器保持一致，否则无法进行循环\n",
    "for vartest in teacher:\n",
    "    num = random.randint(0, 2)  # 每次循环遍历两个随机数\n",
    "    offices[num].append(vartest)\n",
    "i = 1    # 办公室从1开始，不是从0开始\n",
    "for office in offices:\n",
    "    print(f'办公室{i}的人数是{len(office)}, 老师分别是：', end='')   # end='' 表示输出结果\n",
    "    i += 1   # 每次遍历i都要自增1\n",
    "    for test in office:\n",
    "        print(test,end='...')\n",
    "    print('')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
